package frobagroup.services.frontEnd.impl;

import frobagroup.entities.backstage.Product;
import frobagroup.entities.common.Credential;
import frobagroup.entities.frontEnd.User;
import frobagroup.entities.frontEnd.UserProfile;
import frobagroup.repositories.backstage.ProductRepository;
import frobagroup.repositories.frontEnd.UserProfileRepository;
import frobagroup.repositories.frontEnd.UserRepoeitory;
import frobagroup.services.frontEnd.SettingsService;
import frobagroup.utils.MD5;
import frobagroup.utils.fileUtils.FileUploadServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by Tinsley on 2018/5/10.
 */
@Service
public class SettingsServiceImpl implements SettingsService {

    @Autowired
    private UserProfileRepository userProfileRepository;
    @Autowired
    private FileUploadServer fileUploadServer;
    @Autowired
    private UserRepoeitory userRepoeitory;
    @Autowired
    private ProductRepository productRepository;

    @Override
    public UserProfile findProfileById(String uId) {
        return userProfileRepository.findByUId(uId);
    }

    @Override
    public UserProfile updateProfile(UserProfile userProfile,String uId) {
        userProfile.setUId(uId);
        userProfile.setUpdateTime(new Date());
        return userProfileRepository.save(userProfile);
    }

    @Override
    public UserProfile changePortrait(MultipartFile myPhoto, String uId) throws Exception{

        UserProfile userProfile = userProfileRepository.findByUId(uId);
        if (!StringUtils.isEmpty(userProfile.getPhotoPic()))
            fileUploadServer.deletePublicFile(userProfile.getPhotoPic());
        String url = fileUploadServer.uploadFileToCloud(myPhoto);
        userProfile.setPhotoPic(url);
        userProfile.setUpdateTime(new Date());
        return userProfileRepository.save(userProfile);

    }

    @Override
    public Boolean updateUserPassword(Credential credential, String uId) {

        User user = userRepoeitory.findOne(uId);
        if (Objects.equals(user.getPassword(), MD5.encode(credential.getOldPassword()))){
            user.setPassword(MD5.encode(credential.getPassword()));
            user.setUpdateTime(new Date());
            userRepoeitory.save(user);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public List<Product> findFollowProducts(String uId) {
        UserProfile userProfile =  userProfileRepository.findByUId(uId);
        return productRepository.findByStatusAndIdIn("active",userProfile.getFollowProductIds());
    }

    private static String formatFileName(String originalName) {

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd-hhmmssSSS");
        String date = format.format(new Date());
        int lastIndex = originalName.lastIndexOf(".");
        if(lastIndex == -1){
            return originalName + date;
        }else {
            return originalName.substring(0, lastIndex) + date + originalName.substring(lastIndex);
        }
    }
}
